Ray-Sphere intersection

We want to intersect some ray parameterized as $$ R = O_R+tV_R, t > 0$$ with a sphere $$ (x-S_x)^2 + (y-S_y)^2 + (z-S_z)^2 = r_S^2 $$ or written as a vector equation, $$ \| P-O_S \| = r_S $$

We want to find the $t$ in $R$ where $R(t)$ is equal to some $P \in S$, which we can find by substituting $R$ for $P$. $$ \| O_R + tV_R - O_S \| = r_S $$ Is equal to the quadtratic equation: $$ (O_R - O_S + tV_R)^2 = r_S^2 $$ Let us subsititue $ \Delta p $ for $O_R - O_S$, giving: $$ (tV_R + \Delta p)^2 = r_S^2 $$ $$ V_R^2t^2 + 2 \Delta p \cdot V_Rt + \Delta p^2 - r_S^2 = 0 $$ The number of solutions can be found using the discriminant $b^2 - 4ac$, which comes out to: $$ \Delta = (2 \Delta p \cdot V_R)^2 - 4V_R^2 \cdot (\Delta p^2 - r_S^2) $$ If $\Delta < 0$, there is no intersection, if $\Delta = 0$, there is one point of intersection, if $\Delta > 0$, there are two points of intersection.

Just look at the wikipedia article, I came out with the same method, but theirs is cleaner.


In [ ]: